<?php
include "common.php";
class UpImg extends Common {
	function __construct() {
		parent::__construct ();
		$this->load->database ();
	}
	function uploadImg() {
		
		if (! empty ( $_FILES )) {
			$tempFile = $_FILES ['Filedata'] ['tmp_name'];
			$targetPath = $_SERVER ['DOCUMENT_ROOT'] . $_REQUEST ['folder'] . '/';
			//echo $targetPath;
			$folder = $_REQUEST['folder'];
			$arrPath = explode("/", $folder);
			$id  = "";
			foreach ($arrPath as $value){
				$id = $value;
			}
			
			
			//$new_file_name = $this->new_name ( $_FILES ['Filedata'] ['name'] );
			$picname = $_FILES ['Filedata'] ['name'];
			$type = strstr ( $picname, '.' );
			$rand = rand ( 100, 999 );
			//$id =   $this->input->post('lovedog');
			
			$pics = $id . "_" . date ( "YmdHis" ) . $rand . $type;
			
			
			// 上传路径
			
			$pic_path = "./uploadimg/";
			if (is_dir ( $pic_path ) == false) {
				mkdir ( $pic_path, 0777 );
			}
			$pic_path .= $pics;
			move_uploaded_file ( $_FILES ['Filedata'] ['tmp_name'], $pic_path );
			echo "uploadimg/$pics";
			
			
			//move_uploaded_file ( $_FILES ['mypic'] ['tmp_name'], $pic_path );
			
			//move_uploaded_file ( $tempFile,  $targetFile);
			//move_uploaded_file ( $tempFile, iconv ( 'utf-8', 'gbk', $targetFile ) );
			
			//$water = 'water.gif';
			//$img = new image ();
			
			//$img->param ( $targetFile )->water ( $targetFile, $water, 9 );
			
			//echo $this->get_relative_path ( $targetFile );
			//echo $new_file_name;
		}
	}
	
	function new_name($filename){
		$ext = pathinfo($filename);
		$ext = $ext['extension'];
		if ($ext=='jpg'||$ext=='gif'||$ext=='png')
		{
			$name = basename($filename,$ext);
			$name = md5($name.time()).'.'.$ext;
			return $name;
		}
		else
		{
			return;
		}
	}
	
	
	function get_relative_path($path,$dir = 'uploads'){
		return substr($path,strpos($path,$dir),strlen($path ));
	}
}
?>